package com.jishaokang.util;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.jishaokang.cache.ResultCache;
import com.jishaokang.dao.ForbidUserDAO;
import com.jishaokang.dao.UserDAO;
import com.jishaokang.model.dto.ForbidUser;
import com.jishaokang.model.dto.PowerClass;
import com.jishaokang.model.dto.Teacher;
import com.jishaokang.model.dto.User;
import com.jishaokang.model.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import static java.lang.Integer.parseInt;

/**
 * Created by NANA_Final on 2019/6/27.
 */
@Component
public class ForbidUtil {

    @Autowired
    private UserDAO userDAO;
    @Autowired
    private ForbidUserDAO forbidUserDAO;

    public final String SET_TEACHER="{\"allUser\":\"default\",\"userComment\":\"default\",\"allTeacher\":\"allow\",\"teacherAddCourse\":\"allow\",\"teacherAddLive\":\"allow\",\"allExaminer\":\"default\",\"allAdmin\":\"default\"}";
    //
    public PowerClass updateUserPower(int userId,String power) throws ParseException {
        System.out.println(power);
        UserVO user = userDAO.selectByUserId(userId);
        PowerClass powerState = new Gson().fromJson(user.getPower(), PowerClass.class);
        System.out.println(powerState);
        PowerClass powerAction = new Gson().fromJson(power, PowerClass.class);
        System.out.println(powerAction);
        if (powerAction.getAllUser().contains("allow")) powerState.setAllUser(powerAction.getAllUser());
        if (powerAction.getUserComment().contains("allow")) powerState.setUserComment(powerAction.getUserComment());
        if (powerAction.getAllTeacher().contains("allow")) powerState.setAllTeacher(powerAction.getAllTeacher());
        if (powerAction.getTeacherAddCourse().contains("allow")) powerState.setTeacherAddCourse(powerAction.getTeacherAddCourse());
        if (powerAction.getTeacherAddLive().contains("allow")) powerState.setTeacherAddLive(powerAction.getTeacherAddLive());

        user.setPower(new Gson().toJson(powerState));
        User newUser = new User();
        newUser.setUserId(user.getUserId());
        newUser.setPower(user.getPower());
        userDAO.updatePower(newUser);

        ForbidUser forbidUserTemp = new ForbidUser();
        forbidUserTemp.setUserId(userId);
        forbidUserTemp.setPage(1);
        forbidUserTemp.setNumber(10000);
        List<ForbidUser> forbidUsers = forbidUserDAO.selectByUserId(forbidUserTemp);

        for (ForbidUser forbidUser:forbidUsers){
            PowerClass forbidRecord = new Gson().fromJson(forbidUser.getForbidUserFinish(),PowerClass.class);
            if (forbidRecord.getAllUser().contains("forbid") && powerAction.getAllUser().contains("allow")) {
                forbidRecord.setAllUser("allow");
            }
            if (forbidRecord.getUserComment().contains("forbid") && powerAction.getUserComment().contains("allow")){
                forbidRecord.setUserComment("allow");
            }
            if (forbidRecord.getAllTeacher().contains("forbid") && powerAction.getAllTeacher().contains("allow")){
                forbidRecord.setAllTeacher("allow");
            }
            if (forbidRecord.getTeacherAddCourse().contains("forbid") && powerAction.getTeacherAddCourse().contains("allow")){
                forbidRecord.setTeacherAddCourse("allow");
            }
            if (forbidRecord.getTeacherAddLive().contains("forbid") && powerAction.getTeacherAddLive().contains("allow")){
                forbidRecord.setTeacherAddLive("allow");
            }
            forbidUser.setForbidUserFinish(new Gson().toJson(forbidRecord));
            if (!forbidUser.getForbidUserFinish().contains("forbid")) forbidUser.setForbidUserState(1);
            forbidUserDAO.updateForbidUserFinish(forbidUser);
        }

        return refreshUserPower(userId);
    }

    //
    public PowerClass forbidUserPower(int userId,String power) {

        UserVO user = userDAO.selectByUserId(userId);
        PowerClass powerState = new Gson().fromJson(user.getPower(), PowerClass.class);
        PowerClass powerAction = new Gson().fromJson(power, PowerClass.class);
        if (powerAction.getAllUser().contains("forbid")) powerState.setAllUser(powerAction.getAllUser());
        if (powerAction.getUserComment().contains("forbid")) powerState.setUserComment(powerAction.getUserComment());
        if (powerAction.getAllTeacher().contains("forbid")) powerState.setAllTeacher(powerAction.getAllTeacher());
        if (powerAction.getTeacherAddCourse().contains("forbid"))
            powerState.setTeacherAddCourse(powerAction.getTeacherAddCourse());
        if (powerAction.getTeacherAddLive().contains("forbid"))
            powerState.setTeacherAddLive(powerAction.getTeacherAddLive());

        user.setPower(new Gson().toJson(powerState));
        User newUser = new User();
        newUser.setUserId(user.getUserId());
        newUser.setPower(user.getPower());
        userDAO.updatePower(newUser);
        return powerState;
    }


    //
    public PowerClass refreshUserPower(int userId) throws ParseException {
        UserVO user = userDAO.selectByUserId(userId);

        System.out.println(user.getPower());
        PowerClass powerState = new Gson().fromJson(user.getPower(), PowerClass.class);
        if (powerState.getAllUser().equals("allow")) powerState.setAllUser("forbid_-1");
        if (powerState.getUserComment().equals("allow")) powerState.setUserComment("forbid_-1");
        if (powerState.getAllTeacher().equals("allow")) powerState.setAllTeacher("forbid_-1");
        if (powerState.getTeacherAddCourse().equals("allow")) powerState.setTeacherAddCourse("forbid_-1");
        if (powerState.getTeacherAddLive().equals("allow")) powerState.setTeacherAddLive("forbid_-1");

        ForbidUser forbidUserTemp = new ForbidUser();
        forbidUserTemp.setUserId(userId);
        forbidUserTemp.setPage(1);
        forbidUserTemp.setNumber(10000);
        List<ForbidUser> forbidUsers = forbidUserDAO.selectByUserId(forbidUserTemp);

        for (ForbidUser forbidUser:forbidUsers){
            System.out.println(forbidUser);
            PowerClass forbidRecord = new Gson().fromJson(forbidUser.getForbidUserFinish(),PowerClass.class);
            if (forbidRecord.getAllUser().contains("forbid")){
                int timeDifference = getTimeDifference(forbidUser.getForbidUserTime(),new Date());
                if (timeDifference <= getTime(forbidRecord.getAllUser())) {
                    if (getTime(powerState.getAllUser()) >= getTime(forbidRecord.getAllUser())-timeDifference)
                        powerState.setAllUser("forbid_"+(getTime(forbidRecord.getAllUser())-timeDifference));
                }
                else{
                    forbidRecord.setAllUser("allow");
                }
            }
            if (forbidRecord.getUserComment().contains("forbid")){
                int timeDifference = getTimeDifference(forbidUser.getForbidUserTime(),new Date());
                if (timeDifference <= getTime(forbidRecord.getUserComment())) {
                    if (getTime(powerState.getUserComment())>= getTime(forbidRecord.getUserComment())-timeDifference)
                        powerState.setUserComment("forbid_"+(getTime(forbidRecord.getUserComment())-timeDifference));
                }
                else{
                    forbidRecord.setUserComment("allow");
                }
            }
            if (forbidRecord.getAllTeacher().contains("forbid")){
                int timeDifference = getTimeDifference(forbidUser.getForbidUserTime(),new Date());
                System.out.println(timeDifference);
                if (timeDifference <= getTime(forbidRecord.getAllTeacher())) {
                    System.out.println(timeDifference);
                    System.out.println(getTime(forbidRecord.getAllTeacher())-timeDifference);
                    System.out.println(getTime(powerState.getAllTeacher()));
                    if (getTime(powerState.getAllTeacher())>= getTime(forbidRecord.getAllTeacher())-timeDifference)
                        powerState.setAllTeacher("forbid_"+ (getTime(forbidRecord.getAllTeacher())-timeDifference));
                }
                else{
                    forbidRecord.setAllTeacher("allow");
                }
            }
            if (forbidRecord.getTeacherAddCourse().contains("forbid")){
                int timeDifference = getTimeDifference(forbidUser.getForbidUserTime(),new Date());
                if (timeDifference <= getTime(forbidRecord.getTeacherAddCourse())) {
                    if (getTime(powerState.getTeacherAddCourse())>= getTime(forbidRecord.getTeacherAddCourse())-timeDifference)
                        powerState.setTeacherAddCourse("forbid_"+(getTime(forbidRecord.getTeacherAddCourse())-timeDifference));
                }
                else{
                    forbidRecord.setTeacherAddCourse("allow");
                }
            }
            if (forbidRecord.getTeacherAddLive().contains("forbid")){
                int timeDifference = getTimeDifference(forbidUser.getForbidUserTime(),new Date());
                if (timeDifference <= getTime(forbidRecord.getTeacherAddLive())) {
                    if (getTime(powerState.getTeacherAddLive())>= getTime(forbidRecord.getTeacherAddLive())-timeDifference)
                        powerState.setTeacherAddLive("forbid_"+(getTime(forbidRecord.getTeacherAddLive())-timeDifference));
                }
                else{
                    forbidRecord.setTeacherAddLive("allow");
                }
            }
            forbidUser.setForbidUserFinish(new Gson().toJson(forbidRecord));
            if (!forbidUser.getForbidUserFinish().contains("forbid")) forbidUser.setForbidUserState(1);
            forbidUserDAO.updateForbidUserFinish(forbidUser);
        }
        if (powerState.getAllUser().equals("forbid_-1")) powerState.setAllUser("allow");
        if (powerState.getUserComment().equals("forbid_-1")) powerState.setUserComment("allow");
        if (powerState.getAllTeacher().equals("forbid_-1")) powerState.setAllTeacher("allow");
        if (powerState.getTeacherAddCourse().equals("forbid_-1")) powerState.setTeacherAddCourse("allow");
        if (powerState.getTeacherAddLive().equals("forbid_-1")) powerState.setTeacherAddLive("allow");

        user.setPower(new Gson().toJson(powerState));
        User newUser = new User();
        newUser.setUserId(user.getUserId());
        newUser.setPower(user.getPower());
        userDAO.updatePower(newUser);
        return powerState;
    }

    public void updateUserAddExaminer(Integer userId) {
        UserVO user = userDAO.selectByUserId(userId);
        PowerClass powerState = new Gson().fromJson(user.getPower(), PowerClass.class);
        powerState.setAllExaminer("allow");
        user.setPower(new Gson().toJson(powerState));
        User newUser = new User();
        newUser.setUserId(user.getUserId());
        newUser.setPower(user.getPower());
        userDAO.updatePower(newUser);
    }

    public void updateUserDeleteExaminer(Integer userId) {
        UserVO user = userDAO.selectByUserId(userId);
        PowerClass powerState = new Gson().fromJson(user.getPower(), PowerClass.class);
        powerState.setAllExaminer("default");
        user.setPower(new Gson().toJson(powerState));
        User newUser = new User();
        newUser.setUserId(user.getUserId());
        newUser.setPower(user.getPower());
        userDAO.updatePower(newUser);
    }

    public static Integer getTimeDifference(String date,Date date2) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date1 = sdf.parse(date);
        long start = date1.getTime();
        long end = date2.getTime();
        return (int) ((start - end) / (1000 * 60 * 60));
    }

    public static Integer getTime(String string){
        if (!string.contains("forbid")) return 0;
        return parseInt(string.substring(string.indexOf("_")+1));
    }


}